﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace ViewMaker.Core.Silverlight
{
    public class ComboBoxDelKeyBehavior : Behavior<ComboBox>
    {
        public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ComboBoxDelKeyBehavior), new PropertyMetadata(IsEnabledCallback));

        public bool IsEnabled
        {
            get { return (bool)this.GetValue(IsEnabledProperty); }
            set { this.SetValue(IsEnabledProperty, value); }
        }

        private static void IsEnabledCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.KeyDown += new KeyEventHandler(AssociatedObject_KeyDown);
        }

        void AssociatedObject_KeyDown(object sender, KeyEventArgs e)
        {
            if (!IsEnabled) return;
            if (e.Key != Key.Delete) return;
            var comboBox = e.OriginalSource as ComboBox;
            if (comboBox != null) comboBox.SelectedIndex = -1; 
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.KeyDown -= new KeyEventHandler(AssociatedObject_KeyDown);
        }



    }
}
